package com.yingside.calendar;

import java.util.Scanner;

public class Test2 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入年份:");
        int year = input.nextInt();
        System.out.println("请输入月份:");
        int month = input.nextInt();

        int totalDays = 0;
        int monthDay = 0;
        //已知：求日历最重要的，就是
        //1.1月1号固定，现在固定从2017.1.1开始，我们往后推算
        //2.要求得某一年某一月的日历，就需要之前前面总共经历了多少天

        //比如现在要求得2025年11月的日历
        for (int i=2017; i<year; i++){
            if((i % 4 == 0 && i % 100 != 0) || i % 400 == 0){
                totalDays += 366;
            }
            else{
                totalDays += 365;
            }
        }

        for(int i=1; i<=month; i++){
            switch(i){
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    monthDay = 31;
                    break;
                case 2:
                    if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){
                        monthDay = 29;
                    }
                    else{
                        monthDay = 28;
                    }
                    break;
                default:
                    monthDay = 30;
            }

            if(i < month){
                totalDays += monthDay;
            }

        }

        //求余数
        int temp = totalDays % 7;

        System.out.println("日\t一\t二\t三\t四\t五\t六");
        for (int i=1; i<=temp; i++){
            System.out.print("\t");
        }

        for (int i=1; i<=monthDay; i++){
            System.out.print(i + "\t");
            if((totalDays+i) % 7 == 0){
                System.out.println();
            }
        }

    }
}
